package com.itheima.stock.remotedata;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.itheima.stock.common.constance.JsonConstance;
import com.itheima.stock.job.service.StockTimerService;
import com.itheima.stock.mapper.StockMarketIndexInfoMapper;
import com.itheima.stock.mapper.StockMarketLogPriceMapper;
import com.itheima.stock.pojo.StockMarketIndexInfo;
import com.itheima.stock.pojo.StockMarketLogPrice;
import com.itheima.stock.utils.IdWorker;
import com.itheima.util.HttpUtils;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.joda.time.DateTime;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@SpringBootTest
public class PullRemoteData1 {
    @Test
    public void test02() throws ParseException {
        parseStockCodeData("003032");
    }

    public void parseStockCodeData(String code) throws ParseException {
        String host = "https://jsapigpcx.market.alicloudapi.com";
        String path = "/stock/query";
        String method = "GET";
        String appcode = "da297f5b80a341b188c5e7713eae16ba";
        Map<String, String> headers = new HashMap<String, String>();
        //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
        headers.put("Authorization", "APPCODE " + appcode);
        Map<String, String> querys = new HashMap<String, String>();
        querys.put("code", code);
        try {
            HttpResponse response = HttpUtils.doGet(host, path, method, headers, querys);
            System.out.println(response.toString());
            //获取response的body
            System.out.println(EntityUtils.toString(response.getEntity()));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 拉取股票代码
     * @throws ParseException
     */
    @Test
    public void test01() throws ParseException {
        List<String> codeList = new ArrayList<>();
        for (int i = 1; i <= 5; i++) {
            List<String> list = parseStockCode(i, 30);
            if (list!=null && list.size()>0){
                codeList.addAll(list);
            }
        }
        System.out.println("=================");
        System.out.println(codeList);
    }

    public List<String> parseStockCode(int pagenum,int pagesize) throws ParseException {
        List<String> codeList = new ArrayList<>();
        String host = "https://jsapigpcx.market.alicloudapi.com";
        String path = "/stock/list";
        String method = "GET";
        String appcode = "da297f5b80a341b188c5e7713eae16ba";
        Map<String, String> headers = new HashMap<String, String>();
        //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
        headers.put("Authorization", "APPCODE " + appcode);
        Map<String, String> querys = new HashMap<String, String>();
        querys.put("classid", "1");
        // 当前页
        querys.put("pagenum", pagenum+"");
        // 每页显示条数
        querys.put("pagesize", pagesize+"");
        try {
            HttpResponse response = HttpUtils.doGet(host, path, method, headers, querys);
            System.out.println(response.toString());
            //获取response的body
            String resultJson = EntityUtils.toString(response.getEntity());
            Map map = JSON.parseObject(resultJson, Map.class);
            Map map1 = JSON.parseObject(map.get("result").toString(), Map.class);
            JSONArray jsonArray = (JSONArray) map1.get("list");
            for (Object o : jsonArray) {
                JSONObject jsonObj = (JSONObject) o;
                String name = jsonObj.get("name").toString();
                String code = jsonObj.get("code").toString();
                System.out.println(name + " : " +code);
                codeList.add(code);
            }
            return codeList;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }




}
